﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using JT.Domain;
using JT.WEB.Service;
using JT.WEB.Views.Message.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JT.WEB.Areas.JtSiteAdmin.Controllers
{
    [Authorize]
    [Area("JtSiteAdmin")]
    public class MessageController : Controller
    {
        private readonly IUserCenter _usercenter;
        public MessageController(IUserCenter userCenter)
        {
            _usercenter = userCenter;
        }
        // GET: /<controller>/

        public async Task<IActionResult> Index()
        {
            await _usercenter.ReadAsync(EMSLevel.SystemMassage);
            MessageViewModel vm = new MessageViewModel
            {
                Ts = _usercenter.GetMessages(c => c.MSLevel == EMSLevel.SystemMassage)
            };
            return View(vm);
        }

        public async Task<IActionResult> CommentReply()
        {
            await _usercenter.ReadAsync(EMSLevel.CommentReply);

            MessageViewModel vm = new MessageViewModel
            {
                Ts = _usercenter.GetMessages(c => c.MSLevel == EMSLevel.CommentReply)
            };

            return View(vm);
        }


        /// <summary>
        /// 工作通知
        /// </summary>
        /// <returns></returns>
        [Description("工作通知")]
        public async Task<IActionResult> WorkNotice()
        {
            await _usercenter.ReadAsync(EMSLevel.WorkNotice);

            IEnumerable<Message> vm = _usercenter.GetMessages(c => c.MSLevel == EMSLevel.WorkNotice);
            return View(vm);
        }
        /// <summary>
        /// @我的
        /// </summary>
        /// <returns>ViewModel</returns>
        [Description("@我的")]
        public async Task<IActionResult> AtMeAsync()
        {
            await _usercenter.ReadAsync(EMSLevel.AtMe);
            IEnumerable<Message> vm = _usercenter.GetMessages(c => c.MSLevel == EMSLevel.AtMe);
            return View(vm);
        }


        public IActionResult SendMessage()
        {
            return View();
        }
        [HttpPost, ActionName("SendMessage")]
        public async Task<IActionResult> SendMessage(Message message)
        {
            if (ModelState.IsValid)
            {
                int vm = await _usercenter.AddMessage(message);
                if (vm != 0)
                {
                    return RedirectToAction();
                }
                return View(message);
            }
            //Microsoft.AspNetCore.Mvc.ModelBinding.ModelError}
            //var a = ModelState.ValidationState.ToString();
            //ModelState.AddModelError("MSLevel", "消息级别错误");
            return View(message);
        }
        public IActionResult SendedMessages()
        {
            IEnumerable<Message> vm = _usercenter.GetMessages(c => c.Id > 0);
            return View(vm);
        }

    }


}
